<template>
	<view>
		<!-- <view class="bg"></view> -->
		<view class="mine">
			<view class="title">
				<view class="v_t1">
					<view v-if="userData" class="v_name">
						<text class="tt1">Ai用户</text>
						<view class="vt2">
							<text class="tt2">ID:{{ userData.uid }}</text>
							<text class="tt3" @click="onCopyClick(userData.uid)">复制</text>
						</view>
					</view>
					<text v-else class="t_login" @click="onLoginClick">请登陆</text>
				</view>
				<navigator class="nav" url="/src/pages/My/Setting/Setting">
					<uni-icons type="gear" size="30"></uni-icons>
				</navigator>
			</view>
			<view class="middle" v-if="userData">
				<view class="m1">
					<view class="t1">
						<text style="font-weight: bold">{{ userData ? userData.score : '0' }}</text>
						<text class="s1">积分</text>
					</view>

				
						<view class="m2" @click="jumpout('/src/pages/My/Pay/pay?currentIndex=0')">获取积分</view>

					

				</view>
				
				<view class="m_line"></view>
				<view class="mm1" :style="{ height: userData && userData.vip_time ? '186rpx' : '110rpx' }">
					<view class="m_1">
						<view class="m_1_1" v-if="userData && userData.vip_time">
							<view style="display: flex; align-items: center">
								<image class="ii" src="https://sm.2sxzw.com/mgmg/jiabin01/mine/mine_vip_2.png">
								</image>
								<text class="t1" style="margin-left: 16rpx">会员到期时间：</text>
							</view>

							<text class="t2">{{ userData.vip_time }}</text>
						</view>
						<view class="m_1_2" v-else style="display: flex; align-items: center">
							<image class="ii" src="/static/img/VIP1.png"></image>
							<text style="margin-left: 16rpx">普通用户</text>
						</view>
					</view>
					
	
						<view class="m_2" @click="jumpout('/src/pages/My/Pay/pay?currentIndex=1')">获取会员</view>

					


				</view>
			</view>			
			<uni-notice-bar class="c_notice_bar" :speed="speedBar" showIcon  scrollable single :text=lunbo_text v-if="lunbo_text"></uni-notice-bar>
			<view class="bottom">
				<view class="MyListTitle">我的功能</view>
				<view class="MyList">
					<view class="shiping" @click="jumpout('/src/pages/My/JobList/JobList')">
						<image src="/static/img/myicon1.png" class="i1"></image>
						<text class="tt">我的作品</text>
					</view>
					<view class="shiping" @click="onDuiClick">
						<image src="/static/img/myicon2.png" class="i1"></image>
						<text class="tt">兑换券兑换</text>
					</view>
					<view class="shiping" @click="onKeFuClick">
						<image src="/static/img/myicon3.png" class="i1"></image>
						<text class="tt">咨询客服</text>
					</view>
					<view class="shiping" @click="onCheckInClick"  v-if="checkin_reward_score" >
						<image src="/static/img/jifen1.png" class="i1"></image>
						<text class="tt">签到领积分</text>
					</view>
	
				</view>
				

			</view>


		</view>
		<uni-popup ref="popup" type="center">
			<view class="popup-content">
				<uni-icons type="closeempty" size="20" style="align-self: flex-end;" @click="closepop"></uni-icons>
				<view class="popup-content2">
					<uni-icons type="checkbox-filled" size="50" color="#1aad19"></uni-icons>
					<text style="margin-top: 20rpx;font-size: 35rpx;">积分获取成功</text>					
				</view>
			</view>
		</uni-popup>
		
			<view class="RefreshIcon" @click="RefreshBtn" v-if="userData" >刷新</view>
	
	</view>
</template>

<script>
	import {
		getKefuUrl,
		getUser,
		jumpto,
		getAppConfig
	} from '@/src/utils/utils.js';
	
	import {
		get_common_link,
		dui_huan_ma,
		reward_user_look_ad,
		check_in,
	} from '@/src/api/huanlian.js';

	export default {
		data() {
			return {
				userData: null,
				kaifaze_url: null,
				jiaoliuqun_url: null,
				ios_wxpay: false,
				open_daili: false,
				ad_score: null,
				gong_link:null,
				yindao_video_url:null,			
				lunbo_text:null,				
				checkin_reward_score:null,
				lunbo_text:null,
				speedBar:70,
			};
		},
		onLoad() {
			this.initUserData();
			this.get_common_url()
			this.get_app_config()
		},
		async onPullDownRefresh() {
			if (!this.userData) {
				uni.stopPullDownRefresh();
				return;
			}
			try {
				let result = await getUser({}, true);
			} catch (e) {
				console.log(e);
			} finally {
				uni.stopPullDownRefresh();
			}
		},
		methods: {
			RefreshBtn(){
				uni.startPullDownRefresh()
			},
			onDuiClick() {
				let _this = this
				if (!this.userData) {
					uni.navigateTo({
						url: '/src/pages/My/login/login'
					})
					return
				}
				let aa = uni
				// #ifdef MP-ALIPAY
				aa = my
				// #endif
				aa.showModal({
					title: '请输入兑换码',
					editable: true,
					confirmText: '兑换',
					placeholderText: '请输入兑换码',
					success: function(res) {
						if (res.confirm && res.content) {
							_this.duihuan(res.content)
						}
					}
				})
			},
			async duihuan(duihuanma) {
				try {
					let result = await dui_huan_ma({
						duihuanma: duihuanma
					})
					getUser({}, true);
					this.$showToast('兑换成功')
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}

			},
			async get_app_config() {
				try {
					let result = await getAppConfig();
					if (result && result.ad_score) {
						this.ad_score = result.ad_score
					}
					this.checkin_reward_score = result.checkin_reward_score
					this.lunbo_text = result.lunbo_text
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
			},
			onCommonCick(url) {
				if (url) {
					uni.previewImage({
						urls: [url]
					});
				}
			},
			jumpout(url) {
				// #ifdef MP-WEIXIN				
				if ((url.includes('/src/pages/My/Pay/pay')) && getApp().globalData
					.platform == 'ios' && !this.ios_wxpay) {
					if (this.gong_link){
						uni.previewImage({
							urls:[this.gong_link]
						})
					}else{
						uni.showModal({
							showCancel: false,
							content: '由于相关规范，iOS该功能暂不可用，详情请咨询客服'
						});
					}					
					return;
				}
				// #endif

				jumpto(url);
			},
			async initUserData() {
				let _this = this;
				this.userData = getApp().globalData.userData;
				if (!this.userData && uni.getStorageSync('jwt_token')) {
					await getUser();
					this.userData = getApp().globalData.userData;
				}
				uni.$on('userUpdate', (d) => {
					_this.userData = d.userData;
				});
			},

			onLoginClick() {
				uni.navigateTo({
					url: '/src/pages/My/login/login'
				});
			},
			onKeFuClick() {
				getKefuUrl();
			},
			//签到领积分
			async onCheckInClick(){		
				if (!this.userData) {
					uni.navigateTo({
						url: '/src/pages/My/login/login'
					});
					return;
				}
				try{
					let result = await check_in();				
					if (result.err_no == 0){
						getUser({}, true);
						this.showpop()
					}
				}catch(e){
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
				
				
			},
			onCopyClick(t) {
				let _this = this
				uni.setClipboardData({
					data: t,
					showToast: false,
					success: function() {
						_this.$showToast('复制成功')
					}
				});
			},
			async get_common_url() {
				try {
					let result = await get_common_link();
					this.kaifaze_url = result.kaifaze_url
					this.jiaoliuqun_url = result.jiaoliuqun_url
					this.ios_wxpay = result.ios_wxpay
					this.open_daili = result.open_daili
					this.gong_link = result.gong_link					
					this.yindao_video_url = result.yindao_video_url
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}
			},
			
			async rewardUser() {
				try {
					let result = await reward_user_look_ad();
					if (result && result.err_no == 0) {
						getUser({}, true);
						this.showpop()
					}
					
				} catch (e) {
					if (!e) return
					uni.showModal({
						content: e.errMsg || e.message,
						showCancel: false
					});
				}

			},
			showpop() {
				this.$refs.popup.open('center')
			},
			closepop() {
				this.$refs.popup.close()
			},
			showhaoping() {
				this.closepop()
				uni.showModal({
					title: "五星好评永久免费",
					content: "请点击右上角“...”进行评价，\n 给予5星好评！",
					confirmText: this.yindao_video_url?"视频教程":'确定', 
					showCancel: true,
					success: res => {
					    if (res.confirm) {
							if(this.yindao_video_url){
							  uni.navigateTo({
							  	url: '/src/pages/My/ShiPingJieShao/ShiPingJieShao?video_url='+this.yindao_video_url
							  })
							}
					      // 执行确认逻辑
					    } else if (res.cancel) {
					     // console.log('用户取消操作');
					      // 执行取消逻辑
					    }
					  }
				})
			},
			onShareAppMessage(res) { //发送给朋友
				return {
					title: "一键Ai换脸，视频换脸，图片换脸，Ai写真，快来体验吧",
					imageUrl: "https://sm.2sxzw.com/mgmg/jiabin01/mine/fenxiang.jpg",
				}
			},
			onShareTimeline(res) { //分享到朋友圈
				return {
					title: "一键Ai换脸，视频换脸，图片换脸，Ai写真，快来体验吧",
					imageUrl: "https://sm.2sxzw.com/mgmg/jiabin01/mine/fenxiang.jpg",
				}
			},
		}





	};
</script>

<style lang="scss">
	@import 'Mine.scss';
</style>